package tech.lq0.dreamaticvoyage.gui.screen;

import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.Inventory;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import tech.lq0.dreamaticvoyage.Utils;
import tech.lq0.dreamaticvoyage.block.entity.FukamizuTurbineEngineBlockEntity;
import tech.lq0.dreamaticvoyage.gui.menu.FukamizuTurbineEngineMenu;

import java.util.List;
import java.util.Optional;

@OnlyIn(Dist.CLIENT)
public class FukamizuTurbineEngineScreen extends AbstractContainerScreen<FukamizuTurbineEngineMenu> {

    private static final ResourceLocation TEXTURE = Utils.loc("textures/gui/fukamizu_turbine_engine.png");

    public FukamizuTurbineEngineScreen(FukamizuTurbineEngineMenu pMenu, Inventory pPlayerInventory, Component pTitle) {
        super(pMenu, pPlayerInventory, pTitle);
        imageWidth = 176;
        imageHeight = 177;
    }

    @Override
    protected void renderBg(GuiGraphics pGuiGraphics, float pPartialTick, int pMouseX, int pMouseY) {
        int i = (this.width - this.imageWidth) / 2;
        int j = (this.height - this.imageHeight) / 2;
        pGuiGraphics.blit(TEXTURE, i, j, 0, 0, this.imageWidth, this.imageHeight);

        long uceEnergy = FukamizuTurbineEngineScreen.this.menu.getUCEEnergy();
        long feEnergy = FukamizuTurbineEngineScreen.this.menu.getFEEnergy();
        long pressure = FukamizuTurbineEngineScreen.this.menu.getPressure();

        // UCE Energy
        float uceEnergyRate = Mth.clamp((float) uceEnergy / (float) FukamizuTurbineEngineBlockEntity.MAX_UCE_ENERGY, 0, 1);
        pGuiGraphics.blit(TEXTURE, i + 50, j + 69 - (int) (39 * uceEnergyRate),
                186, 0, 10, (int) (39 * uceEnergyRate));

        // FE Energy
        float feEnergyRate = Mth.clamp((float) feEnergy / (float) FukamizuTurbineEngineBlockEntity.MAX_FE_ENERGY, 0, 1);
        pGuiGraphics.blit(TEXTURE, i + 116, j + 69 - (int) (39 * feEnergyRate),
                197, (int) (39 * (1 - feEnergyRate)), 10, (int) (39 * feEnergyRate));

        // Pressure
        float pressureRate = Mth.clamp((float) (pressure - FukamizuTurbineEngineBlockEntity.MIN_PRESSURE) /
                (float) (FukamizuTurbineEngineBlockEntity.MAX_PRESSURE - FukamizuTurbineEngineBlockEntity.MIN_PRESSURE), 0, 1);
        pGuiGraphics.blit(TEXTURE, i + 28, j + 64 - (int) (30 * pressureRate), 177, 0, 8, 3);
    }

    @Override
    protected void renderTooltip(GuiGraphics pGuiGraphics, int pX, int pY) {
        super.renderTooltip(pGuiGraphics, pX, pY);

        int i = (this.width - this.imageWidth) / 2;
        int j = (this.height - this.imageHeight) / 2;

        int pressure = (int) FukamizuTurbineEngineScreen.this.menu.getPressure();

        var pressureComponent = Component.translatable("des.dreamaticvoyage.pressure",
                FukamizuTurbineEngineScreen.this.menu.getPressure(), FukamizuTurbineEngineBlockEntity.MIN_PRESSURE, FukamizuTurbineEngineBlockEntity.MAX_PRESSURE);

        int uceCost = pressure >= 32 ? FukamizuTurbineEngineBlockEntity.DEFAULT_UCE_COST + (pressure - 32) * 2 : 0;
        int feSpeed = pressure >= 32 ? FukamizuTurbineEngineBlockEntity.DEFAULT_FE_SPEED + (pressure - 32) * 16 : 0;

        var speedComponent = Component.literal(uceCost + " UCE/t => " + feSpeed + " FE/t").withStyle(ChatFormatting.GREEN);

        if ((pX - i) >= 26 && (pX - i) <= 32 && (pY - j) >= 34 && (pY - j) <= 67) {
            pGuiGraphics.renderTooltip(this.font, List.of(pressureComponent, speedComponent),
                    Optional.empty(), pX, pY);
        }

        if ((pX - i) >= 49 && (pX - i) <= 61 && (pY - j) >= 29 && (pY - j) <= 70) {
            pGuiGraphics.renderTooltip(this.font, List.of(Component.translatable("des.dreamaticvoyage.umisu_current_energy",
                    FukamizuTurbineEngineScreen.this.menu.getUCEEnergy(), FukamizuTurbineEngineBlockEntity.MAX_UCE_ENERGY)), Optional.empty(), pX, pY);
        }

        if ((pX - i) >= 115 && (pX - i) <= 127 && (pY - j) >= 29 && (pY - j) <= 70) {
            pGuiGraphics.renderTooltip(this.font, List.of(Component.literal(FukamizuTurbineEngineScreen.this.menu.getFEEnergy()
                    + " / " + FukamizuTurbineEngineBlockEntity.MAX_FE_ENERGY + " FE")), Optional.empty(), pX, pY);
        }
    }

    @Override
    public void render(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
        this.renderBackground(pGuiGraphics);
        super.render(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
        this.renderTooltip(pGuiGraphics, pMouseX, pMouseY);
    }

    @Override
    protected void init() {
        super.init();
        this.titleLabelX = 8;
        this.titleLabelY = 2;
        this.inventoryLabelX = 8;
        this.inventoryLabelY = 85;
    }
}
